import gcoord from "../../miniprogram_npm/gcoord/dist/gcoord";
Page({
  data: {
    // 当前的位置
    latitude: "",
    longitude: "",
    // 画圆
    circles: [],
    chanyeApiList: [], // 企业后台接口信息集合
    // markers: [], // 企业标记点集合
    markers: [],
    activeIndex: 0, // 默认的产业选择
    showTabName: "", // 顶部显示的产业名称
    mapCtx: null, // 地图实例对象
    totalQy: "", // 顶部数量
    topImg: "", // 顶部图标
  },

  // 页面解析完成后执行
  onReady: function () {
    // 步骤一、常见地图实例对象
    this.mapCtx = wx.createMapContext("hotMap");
  },
  onLoad: function (e) {
    const that = this;
    getApp().globalData.getLocation({
      type: "gcj02",
      success(res) {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
        });
        // 数据初始化
        that.initInfos();
      },
    });
  },

  /**
   * methods
   */
  // 初始化数据
  async initInfos() {
    this.getkeyIndustryCount();
    // 初始化markers;
    this.initMarkers();
  },

  // 获取产业的分布
  getkeyIndustryCount() {
    const data = [{
        location: {
          lat: "31.133259",
          lng: "120.643583"
        },
        region: "吴江区",
      },
      {
        location: {
          lat: "31.656016",
          lng: "120.752512"
        },
        region: "常熟市",
      },
      {
        location: {
          lat: "31.846958",
          lng: "120.563915"
        },
        region: "张家港市",
      },
      {
        location: {
          lat: "31.329631",
          lng: "120.433987"
        },
        region: "高新区",
      },
      {
        location: {
          lat: "31.458101",
          lng: "121.130366"
        },
        region: "太仓市",
      },
      {
        location: {
          lat: "31.273814",
          lng: "120.631414"
        },
        region: "吴中区",
      },
      {
        location: {
          lat: "31.385476",
          lng: "120.980795"
        },
        region: "昆山市",
      },
      {
        location: {
          lat: "31.32466",
          lng: "120.599241"
        },
        region: "姑苏区",
      },
      {
        location: {
          lat: "31.324235",
          lng: "120.718329"
        },
        region: "工业园区",
      },
      {
        location: {
          lat: "31.369189",
          lng: "120.642391"
        },
        region: "相城区",
      },
      {
        location: {
          lat: "31.524564",
          lng: "120.563243"
        },
        region: "苏州市",
      },
    ];
    // console.log(params, "params");
    // console.log(data, "全部的产业分布");
    this.setData({
      chanyeApiList: data,
    });
  },

  // 初始化点标记
  initMarkers() {
    const markers = [];
    this.data.chanyeApiList.forEach((val, index) => {
      if (val.location) {
        markers.push({
          id: index,
          width: 0,
          height: 0,
          latitude: val.location.lat,
          longitude: val.location.lng,
          iconPath: "https://zx-1311146360.cos.ap-shanghai.myqcloud.com/version01/mapicon.png",
          // "/static/mapicon.png",
          label: {
            id: index,
            content: `${val.region}`,
            fontSize: val.region === "苏州市" ? 14 : 11,
            width: val.region === "苏州市" ? 100 : 50,
            height: val.region === "苏州市" ? 60 : 50,
            borderWidth: 2,
            borderColor: val.region === "苏州市" ? "#fff" : "#fff",
            bgColor: val.region === "苏州市" ? "#EA8967" : "#5d9be7",
            color: val.region === "苏州市" ? "#fff" : "#fff",
            borderRadius: val.region === "苏州市" ? 20 : 50,
            textAlign: "center",
            anchorX: 0,
            anchorY: -18,
          },
        });
      }
    });
    this.setData({
      markers: markers,
    });
  },
  // 坐标系转化
  transMarker(item) {
    const resae = gcoord.transform(
      [item[0], item[1]],
      gcoord.GCJ02, // 当前坐标系
      gcoord.BD09 // 目标坐标系
    );
    return resae;
  },

  // 点击区域
  async markerCalloutTap(e) {
    const region = this.data.chanyeApiList[e.markerId];
    // if (
    //   e.markerId === 0 ||
    //   e.markerId === 2 ||
    //   e.markerId === 3 ||
    //   e.markerId === 5
    // )
    //   return;
    // ===============
    const vipCode = await getApp().globalData.checkVipStatus()
    if (vipCode === 'ordinary' || vipCode === 'notVipBindFI' || vipCode === 'visitor') {
      wx.showModal({
        title: '提示',
        content: '该功能为VIP会员功能，请联系客服了解详情。',
        showCancel: false,
        confirmText: '确定',
        success: function () {}
      })
      return
    }
    wx.navigateTo({
      url: `/pages/cyHotDetail/cyHotDetail?latitude=${region.location.lat}&longitude=${region.location.lng}&region=${region.region}`,
    });
  },

  // 点击事件
  clickCy(e) {
    this.setData({
      activeIndex: e.currentTarget.dataset.index,
    });
    this.initInfos();
  },

  onShareAppMessage: function (res) {},
});